<?php

namespace Home\Controller;

use Think\Controller;

class UserController extends Controller {

    
    //完善个人信息
    public function edit($shopName="",$realName="",$phone="",$province="",$city="",$street=""){
        if(IS_POST){
            $userid = session("userid");
            $data = array('realName'=>$realName,'phone'=>$phone,'province'=>$province,'city'=>$city,'street'=>$street,'shopName'=>$shopName,'createdate'=>NOW_TIME,'logindate'=>NOW_TIME);
            $map = array('id'=>$userid);
            M('buyers')->where($map)->save($data);
            
            $dataAddr = array('uid'=>$userid,'name'=>$realName,'phone'=>$phone,'province'=>$province,'city'=>$city,
                'street'=>$street,'shopName'=>$shopName,'createdate'=>NOW_TIME,'lastupdate'=>NOW_TIME);
            M('addr')->add($dataAddr);
            session('userphone',$phone);
            $this->redirect("index/cart");
        }
        $this->display();
    }
    
   
    //跳转进入微信授权页面
    public function jumpWXlogin() {
        header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx9b58617b10123d97&redirect_uri=http://m.shiguo365.com/index.php/Home/User/oauth2&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
    }

    //微信授权会跳页面
    public function oauth2($code) {
         
        if(isset($code)){
            $appid = "wx9b58617b10123d97";
            $appsecret = "c7912853d8019252dc0bc16a1a3a18b4";
            //获取openid
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
            $result = $this->https_request($url);

           //echo $result;
            $WXOPEN = json_decode($result, true);
            $token = $WXOPEN["access_token"];

            $openid = $WXOPEN["openid"];

            $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid&lang=zh_CN";
            $user =(array) json_decode($this->https_request($url));  
            $map['openid']=$openid;
            $uid=M('buyers')->where($map)->getField('id');
            dump($uid);
            $userphone=M('buyers')->where($map)->getField('phone');
            
            
            if($uid){
                session('userlogo',$user['headimgurl']); 
                session('useropenid',$openid);
                session('userid',$uid);
                session('wxuser',$user);       
                session('userphone',$userphone);
                $this->redirect("index/index");
            }else{
                //如果没有注册，自动注册
                session('userlogo',$user['headimgurl']); 
                $data['openid']=$openid;
                $data['nickName']=$user['nickname'];
                $data['createdate']=NOW_TIME;
                $uid=M('buyers')->add($data);
                session('useropenid',$openid);
                session('userid',$uid);
                session('userphone',$userphone);
                session('wxuser',$user);
                var_dump($user);
                $this->redirect("index/index");
            }
           
        }
    }

    
    //http工具
    function https_request($url, $data = null) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

}
